<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="/static/component/pear/css/pear.css" />
  </head>
  <body class="pear-container">
    <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">

                <div class="layui-form-item">

                  <style type="text/css">
                    .layui-form-item .layui-inline {
                        margin-bottom: 5px;
                        margin-right: 0px;
                    }
                  </style>

                    <label class="layui-form-label">筛选条件:</label>
                    <div class="layui-inline" style="width: 200px;">
                      <div class="layui-input-inline">
                        <input type="text" name="adminname" placeholder="请输入" autocomplete="off" class="layui-input">
                      </div>
                    </div>
                 
                  <div class="layui-inline">

                    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="search">

                      <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>

                    </button>

                    

                  </div>

                </div>

              </div>
      <div class="layui-card-body">
        <table id="dataTable" lay-filter="dataTable"></table>
      </div>
    </div>

    <script type="text/html" id="toolbar">
      <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>
    </script>
    {php} 
        $admin=session("admin");
        $juese=$admin['juese'];
        $id=$admin['id'];
      {/php}
    <script type="text/html" id="options">
          {{#  if(d.id != {$id} && {$juese}==1){ }}   
          <!-- 超级管理员 操作别人 -->
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
            <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="xiu">密码</button>
            <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="del"><i class="layui-icon layui-icon-delete"></i></button>
          {{#  } else if(d.id == {$id} && {$juese}!=1) { }}
          <!-- 普通管理员改自己 -->
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
            <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="xiu">密码</button>
          {{#  } else if(d.id == {$id} && {$juese}==1) { }}
          <!-- 超级管理员改自己 -->
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
            <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="xiu">密码</button>
          {{#  } }}
    </script>

    <script src="/static/component/layui/layui.js"></script>
    <script src="/static/component/pear/pear.js"></script>
    <script>
      layui.use(['table', 'form', 'jquery','common','laydate'], function() {
        let table = layui.table;
                let form = layui.form;
        let laydate = layui.laydate;
        let $ = layui.jquery;
        let common = layui.common;
                 laydate.render({ 
                  elem: '#date',
                  type:'date'
                  ,range: '~' //或 range: '~' 来自定义分割字符
                });
        let MODULE_PATH = "/admin/admin/";

        let cols = [
          [  
              {
                  field: 'id',
                  title: 'ID', 
                  sort: true, 
                  unresize: true,
                  align: 'center'
              },{
                  field: 'juese',
                  title: '角色', 
                  sort: true, 
                  unresize: true,
                  align: 'center',
                  templet:function(d){
                      return d.juese
                  }
              },{
                  field: 'adminname',
                  title: '登录名',
                  unresize: true,
                  align: 'center'
              },{
                  field: 'name',
                  title: '姓名',
                  unresize: true,
                  align: 'center'
              },{
                  field: 'phone',
                  title: '联系电话',
                  unresize: true,
                  align: 'center'
              },{
                  field: 'addtime',
                  title: '创建时间',
                  unresize: true,
                  align: 'center',
                  width:170
              },{
                  title: '操作',
                  toolbar: '#options',
                  unresize: true,
                  align: 'center',
                  width: 180
              }
          ]
        ]

        table.render({
          elem: '#dataTable',
          url: MODULE_PATH + 'lst',
          page: true, 
          cols: cols,
                    cellMinWidth: 100,
          skin: 'line',
                    type:'post',
          toolbar: '#toolbar',
                    method: 'post'
          ,defaultToolbar: [{
            title: '刷新',
            layEvent: 'refresh',
            icon: 'layui-icon-refresh',
          }, 'filter', 'print', 'exports']
        });
        var where={name:"",type:"",time:"",arr:''};
        form.on('submit(search)', function(data){

            where = data.field;

            console.log(where);

            //执行重载

            table.reload('dataTable', {

              where: where
            });

        });
        table.on('tool(dataTable)', function(obj){
        var data = obj.data;
        var arr_id=[];
        arr_id.push(data.id);
        console.log(arr_id);
        if(obj.event === 'del'){
          layer.confirm('真的要删除吗', function(index){
            //执行 Ajax 后重载
            $.ajax({
               type: "POST",
               url: "{:url('/admin/admin/do_del')}",
               data:{id:arr_id},
               success: function(msg){
                if(msg>0){
                 layer.msg('删除成功', {
                      offset: '15px'
                      ,icon: 1
                      ,time: 1000
                    });
                }else{
                  layer.msg('删除失败', {
                      offset: '15px'
                      ,icon: 2
                      ,time: 1000
                    });
                }
               }
            });
              obj.del();
              layer.close(index);
            });
        } else if(obj.event === 'edit'){
          var tr = $(obj.tr);
          var id=data.id;
          layer.open({
            type: 2
            ,title: '编辑管理员'
            ,content: "{:url('/admin/admin/update')}?do=update&id="+id
            ,maxmin: true
            ,area: ['70%', '80%']
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
              var iframeWindow = window['layui-layer-iframe'+ index]
              ,submitID = 'LAY-user-front-submit'
              ,submit = layero.find('iframe').contents().find('#'+ submitID);

              //监听提交
              iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                var field = data.field; //获取提交的字段
                
                //提交 Ajax 成功后，静态更新表格中的数据
                $.ajax({
                   type: "POST",
                   url: "{:url('/admin/admin/do_update')}?id="+id,
                   data: field,
                   success: function(msg){
                     // alert( "Data Saved: " + msg );
                     if(msg==1){
                        layer.msg('修改成功', {
                          offset: '15px'
                          ,icon: 1
                          ,time: 1000
                        });
        layer.close(index); //关闭弹层
        table.reload('dataTable');//数据刷新
                     }else if(msg==3){
                      layer.msg('该管理员账号已存在', {
                        offset: '15px'
                        ,icon: 2
                        ,time: 1000
                      });
                     }else if(msg==5){
                      layer.msg('请选择省市', {
                        offset: '15px'
                        ,icon: 2
                        ,time: 1000
                      });
                     }else if(msg==6){
                      layer.msg('当前城市已有商务据人员', {
                        offset: '15px'
                        ,icon: 2
                        ,time: 1000
                      });
                     }else if(msg==0){
                      layer.msg('修改成功', {
                        offset: '15px'
                        ,icon: 1
                        ,time: 1000
                      });
                      layer.close(index); //关闭弹层
                    table.reload('dataTable');//数据刷新
                     }else{
                      layer.msg('修改失败', {
                        offset: '15px'
                        ,icon: 2
                        ,time: 1000
                      });
                     }
                   }
                });
                //table.reload('LAY-user-front-submit'); //数据刷新
                // table.reload('dataTable', {
                //   where: field
                //   ,url:'{:url(\'/admin/admin/lst\')}'
                //   ,contentType: 'application/json'
                //   ,page: true
                // });

              });  
              
              submit.trigger('click');
            }
            ,success: function(layero, index){
              
            }
          });
        }else{
          var tr = $(obj.tr);
          var id=data.id;
          layer.open({
            type: 2
            ,title: '修改密码'
            ,content: "{:url('/admin/admin/pass')}?id="+id
            ,maxmin: true
            ,area: ['50%', '80%']
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
              var iframeWindow = window['layui-layer-iframe'+ index]
              ,submitID = 'LAY-user-front-submit'
              ,submit = layero.find('iframe').contents().find('#'+ submitID);

              //监听提交
              iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                var field = data.field; //获取提交的字段
                
                //提交 Ajax 成功后，静态更新表格中的数据
                $.ajax({
                   type: "POST",
                   url: "{:url('/admin/admin/xiu_pass')}?id="+id,
                   data: field,
                   success: function(msg){
                     // alert( "Data Saved: " + msg );
                     if(msg==1){
                        layer.msg('修改成功', {
                          offset: '15px'
                          ,icon: 1
                          ,time: 1000
                        });
                    layer.close(index); //关闭弹层
                    table.reload('dataTable');//数据刷新
                     }else if(msg==3){
                      layer.msg('两次密码输入不一致', {
                        offset: '15px'
                        ,icon: 2
                        ,time: 1000
                      });
                     }else{
                      layer.msg('修改失败,新旧密码一致', {
                        offset: '15px'
                        ,icon: 2
                        ,time: 1000
                      });
                     }
                   }
                });
                //table.reload('dataTable'); //数据刷新
                // table.reload('dataTable', {
                //   where: field
                //   ,url:'{:url(\'/admin/admin/lst\')}'
                //   ,contentType: 'application/json'
                //   ,page: true
                // });

              });  
              
              submit.trigger('click');
            }
            ,success: function(layero, index){
              
            }
          });
        }
    });

        table.on('toolbar(dataTable)', function(obj) {
          if (obj.event === 'add') {
            window.add();
          } else if (obj.event === 'refresh') {
            window.refresh();
          }
        });
        window.add = function() {
          layer.open({
          type: 2
          ,title: '添加管理员'
          ,content: "{:url('/admin/admin/add')}?do=add"
          ,maxmin: true
          ,area: ['70%', '80%']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
            ,submitID = 'LAY-user-front-submit'
            ,submit = layero.find('iframe').contents().find('#'+ submitID);

            //监听提交
            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
              var field = data.field; //获取提交的字段
              // layer.alert(JSON.stringify(data.field), {
              //   title: '最终的提交信息'
              // })
              
              $.ajax({
               type: "POST",
               url: "{:url('/admin/admin/do_add')}",
               data: field,
               success: function(msg){
                 if(msg==1){
                    layer.msg('添加成功', {
                      offset: '15px'
                      ,icon: 1
                      ,time: 1000
                    });
                layer.close(index); //关闭弹层
                table.reload('dataTable');//数据刷新
                 }else if(msg==2){
                  layer.msg('该管理员账号已存在', {
                    offset: '15px'
                    ,icon: 2
                    ,time: 1000
                  });
                 }else if(msg==4){
                  layer.msg('两次输入密码不一致', {
                    offset: '15px'
                    ,icon: 2
                    ,time: 1000
                  });
                 }else if(msg==5){
                  layer.msg('请选择省市', {
                    offset: '15px'
                    ,icon: 2
                    ,time: 1000
                  });
                 }else if(msg==6){
                  layer.msg('当前城市已有商务据人员', {
                    offset: '15px'
                    ,icon: 2
                    ,time: 1000
                  });
                 }else{
                    layer.msg('添加失败', {
                    offset: '15px'
                    ,icon: 2
                    ,time: 1000
                  });
                 }
               }
            });
               
            });  
            
            submit.trigger('click');
          }
          // ,cancel: function(index, layero){ 
          //   if(confirm('确定要关闭么')){ //只有当点击confirm框的确定时，该层才会关闭
          //     layer.close(index)
          //   }
          //   return false; 
          // }
        });
        }

        window.refresh = function(param) {
          table.reload('dataTable');
        }
    })
    </script>
  </body>
</html>
